package com.xmy.erp.controller;

import com.xmy.erp.entity.*;
import com.xmy.erp.entity.response.ResLogin;
import com.xmy.erp.service.*;
import com.xmy.study.util.Code;
import com.xmy.study.util.Result;
import com.xmy.study.util.ResultCode;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 登录账号 前端控制器
 * </p>
 *
 * @author min
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/api/erp")
@Scope("prototype")
public class AdminController{

    @Autowired
    IAdminService iAdminService;

    @Autowired
    IRoleService iRoleService;

    @Autowired
    IRoleAdminService iRoleAdminService;

    @Autowired
    IPermissionService iPermissionService;

    @Autowired
    ICompanyService iCompanyService;

    @PostMapping(value = "/login")
    public Result login(@RequestBody Map<String,Object> reMap){
        //构造登录令牌
        try {
            String username = (String) reMap.get("username");
            String password = (String) reMap.get("password");
            password = new Md5Hash(password,username,3).toString();
            UsernamePasswordToken upToken = new UsernamePasswordToken(username,password);
            Subject subject = SecurityUtils.getSubject();
            //2.调用subject进行登录
            subject.login(upToken);//获取session
            String sid = (String) subject.getSession().getId();
            Map<String,Object> data = new HashMap<>();
            data.put("token",sid);
            //long timeout = SecurityUtils.getSubject().getSession().getTimeout();
            //System.out.println(timeout+"毫秒");
            return new Result(Code.OK,data);

        }catch (Exception e) {
            //return new Result(Code.ERROR,e.getMessage());
            //return new Result(Code.OK,data);
            return new Result(ResultCode.MOBILEORPASSWORDERROR);
        }
    }

    public Result login(@RequestBody ResLogin resLogin){
        //构造登录令牌
//        try {
            String username = resLogin.getUserName();
            String password = resLogin.getPassWord();
        System.out.println("password1:"+password);
            password = new Md5Hash(password,username,3).toString();
        System.out.println("password2:"+password);
            UsernamePasswordToken upToken = new UsernamePasswordToken(username,password);
        System.out.println("password3:"+password);

            Subject subject = SecurityUtils.getSubject();
            //2.调用subject进行登录
            subject.login(upToken);//获取session

            String sid = (String) subject.getSession().getId();
            Map<String,Object> data = new HashMap<>();
            data.put("token",sid);

            long timeout = SecurityUtils.getSubject().getSession().getTimeout();
            //System.out.println(timeout+"毫秒");

            //1.subject获取所有的安全数据集合
            PrincipalCollection principals = subject.getPrincipals();
            if(principals != null && !principals.isEmpty()){
                //2.获取安全数据
                Admin admin = (Admin) principals.getPrimaryPrincipal();
                if (admin.getIsLogin().equals(0)){
                    return new Result(Code.ERROR,"","系统维护中，稍后再试");
                }
            }

            //设置为新生或者老生
            Company company = iCompanyService.getById(1L);
            if(company !=null){
                if(company.getIsMaintain() == 1){
                    if(username.equals("admin")){
                        return new Result(Code.OK,data,"");
                    }else{
                        return new Result(Code.ERROR,"","系统维护中……");
                    }
                }else {
                    return new Result(Code.OK,data,"");
                }
            }else {
                return new Result(Code.OK,data,"");
            }

            //return new Result(Code.OK,data);

//        }catch (Exception e) {
//            //return new Result(Code.ERROR,e.getMessage());
//            //return new Result(Code.OK,data);
//            return new Result(ResultCode.MOBILEORPASSWORDERROR,"");
//        }
    }


    @PostMapping(value = "/auth/logout")
    public Result logout(@RequestBody Map<String,Object> reMap){
        return new Result(Code.OK);
    }


    @GetMapping("/admin/get_info")
    public Result getInfo(){
        return new Result(Code.OK,"","");
    }

}
